Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
aggregate-error
Advanced tools
The aggregate-error package is used to create an Error instance that aggregates multiple errors into a single error object. This can be useful when you have several operations that may each throw errors, and you want to collect all of those errors into a single error object that can be thrown, returned, or logged.
Aggregating multiple errors
This feature allows you to collect multiple errors into a single error object. The code sample demonstrates how to create an instance of AggregateError with an array of Error objects and then throw it.
const AggregateError = require('aggregate-error');
const errors = [
new Error('First error'),
new Error('Second error')
];
const aggregatedError = new AggregateError(errors);
throw aggregatedError;
Iterating over aggregated errors
This feature allows you to iterate over the individual errors within an AggregateError instance. The code sample shows how to loop through each error using a for...of loop and log them to the console.
const AggregateError = require('aggregate-error');
const errors = [
new Error('First error'),
new Error('Second error')
];
const aggregatedError = new AggregateError(errors);
for (const error of aggregatedError) {
console.error(error);
}
The verror package provides a way to create rich JavaScript errors. It allows you to chain errors, add context to them, and format multi-line error messages. It is similar to aggregate-error in that it helps manage multiple error objects, but it focuses more on providing additional context and information about single errors rather than aggregating multiple errors.
Multi-error is another package that allows you to combine multiple errors into a single error object. It is similar to aggregate-error, but it has a different API and additional features such as filtering and mapping over the contained errors.
Create an error from multiple errors
Note: With Node.js 15, there's now a built-in AggregateError
type.
npm install aggregate-error
import AggregateError from 'aggregate-error';
const error = new AggregateError([new Error('foo'), 'bar', {message: 'baz'}]);
throw error;
/*
AggregateError:
Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:33)
Error: bar
at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
Error: baz
at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
at AggregateError (/Users/sindresorhus/dev/aggregate-error/index.js:19:3)
at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
*/
for (const individualError of error.errors) {
console.log(individualError);
}
//=> [Error: foo]
//=> [Error: bar]
//=> [Error: baz]
Returns an Error
.
Type: Array<Error|object|string>
If a string, a new Error
is created with the string as the error message.
If a non-Error object, a new Error
is created with all properties from the object copied over.
FAQs
Create an error from multiple errors
The npm package aggregate-error receives a total of 21,945,537 weekly downloads. As such, aggregate-error popularity was classified as popular.
We found that aggregate-error demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.